using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
using UnityEngine.Events;

public class EntryLogic : MonoBehaviour
{
    private bool bShowJP = true;
    private bool bShowCredit = false;
    private bool bMuted = false;

    // Start is called before the first frame update
    void Start()
    {
        Time.timeScale = 1.0f;
        // Bind Events
        {
            GameObject ButtonObject = GameObject.Find("UI/Canvas/Panel_StartGame/Button_Start");
            Button ButtonStart = ButtonObject.GetComponent<Button>();
            ButtonStart.onClick.AddListener(delegate ()
            {
                AudioManager.Instance.PlaySE("SE_03_Button");
                GoNoNextScene();
            });

            ButtonObject = GameObject.Find("UI/Canvas/Panel_StartGame/Button_Quit");
            Button ButtonQuit = ButtonObject.GetComponent<Button>();
            ButtonQuit.onClick.AddListener(delegate ()
            {
                QuitGame();
            });

            // 语言调整
            ButtonObject = GameObject.Find("UI/Canvas/Panel_StartGame/Button_Language");
            Button ButtonLanguage = ButtonObject.GetComponent<Button>();
            ButtonLanguage.onClick.AddListener(delegate ()
            {
                bShowJP = !bShowJP;
                UpdateLanguage();
                AudioManager.Instance.PlaySE("SE_03_Button");
            });

            // 音量调整
            ButtonObject = GameObject.Find("UI/Canvas/Panel_StartGame/Button_Music");
            Button ButtonMusic = ButtonObject.GetComponent<Button>();
            ButtonMusic.onClick.AddListener(delegate ()
            {
                bMuted = !bMuted;
                if (bMuted)
                    AudioManager.Instance.MuteAll();
                else
                {
                    AudioManager.Instance.UnMuteAll();
                    AudioManager.Instance.PlaySE("SE_03_Button");
                }
            });

            // 制作人名单
            ButtonObject = GameObject.Find("UI/Canvas/Panel_StartGame/Button_Credit");
            Button ButtonCredit = ButtonObject.GetComponent<Button>();
            ButtonCredit.onClick.AddListener(delegate ()
            {
                bShowCredit = !bShowCredit;
                UpdateCreditList();
                AudioManager.Instance.PlaySE("SE_03_Button");
            });
        }

        UpdateLanguage();
        AudioManager.Instance.PlayerBGM1();
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Escape))
        {
            QuitGame();
            return;
        }
        else if(Input.GetKeyDown(KeyCode.U) || Input.GetKeyDown(KeyCode.I)
        || Input.GetKeyDown(KeyCode.J)
        || Input.GetKeyDown(KeyCode.K)
        || Input.GetKeyDown(KeyCode.Keypad2)
        || Input.GetKeyDown(KeyCode.Keypad5)
        || Input.GetKeyDown(KeyCode.Keypad3)
        || Input.GetKeyDown(KeyCode.Keypad6))
        {
            GoNoNextScene();
        }
    }
    private void QuitGame()
    {
        AudioManager.Instance.PlaySE("SE_03_Button");
#if UNITY_EDITOR
        UnityEditor.EditorApplication.isPlaying = false;
#else
                Application.Quit();
#endif
    }

    private void UpdateLanguage()
    {
        var TitleJa = GameObject.Find("UI/Canvas/Panel_StartGame/Panel_Title_Ja");
        TitleJa.SetActive(bShowJP);
        var TitleZh = GameObject.Find("UI/Canvas/Panel_StartGame/Panel_Title_Zh");
        TitleZh.SetActive(!bShowJP);
    }
    private void UpdateCreditList()
    {
        var ImgCreditList = GameObject.Find("UI/Canvas/Panel_StartGame/Img_Credit_List");
        ImgCreditList.SetActive(bShowCredit);
    }

    private void GoNoNextScene()
    {
        FXManager.Instance.ShowUIFX(0);
    }
}
